package com.gdk.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.gdk.subject.application.convert.SubjectAnswerDTOConverter;
import com.gdk.subject.application.convert.SubjectCategoryDTOConverter;
import com.gdk.subject.application.convert.SubjectInfoDTOConverter;
import com.gdk.subject.application.dto.SubjectCategoryDTO;
import com.gdk.subject.application.dto.SubjectInfoDTO;
import com.gdk.subject.common.entity.PageResult;
import com.gdk.subject.common.entity.Result;
import com.gdk.subject.domain.entity.SubjectAnswerBO;
import com.gdk.subject.domain.entity.SubjectCategoryBO;
import com.gdk.subject.domain.entity.SubjectInfoBO;
import com.gdk.subject.domain.service.SubjectCategoryDomainService;
import com.gdk.subject.domain.service.SubjectInfoDomainService;
import com.gdk.subject.infra.basic.entity.SubjectCategory;
import com.gdk.subject.infra.basic.service.SubjectCategoryService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 *
 * @author Gudong~
 * @create 2024-10-20 15:56
 */

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名称不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOToInfoBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE
                    .convertDTOToAnswerBOList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        }catch (Exception e) {
            log.error("SubjectController.add.dto:{}", e.getMessage(),e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询分页列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOToInfoBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boPageResult);
        }catch (Exception e) {
            log.error("SubjectController.getSubjectPage.dto:{}", e.getMessage(),e);
            return Result.fail("查询分页列表失败");
        }
    }

    /**
     * 查询分页信息
     */
    @PostMapping("/querySubjectInfo")
    public Result<PageResult<SubjectInfoDTO>> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOToInfoBO(subjectInfoDTO);
            SubjectInfoBO boResult = subjectInfoDomainService.getSubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoResult = SubjectInfoDTOConverter.INSTANCE.convertBOToInfoDTO(boResult);
            return Result.ok(dtoResult);
        }catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.dto:{}", e.getMessage(),e);
            return Result.fail("查询分页信息失败");
        }
    }

}
